"""iOS Coverage Manager"""

import argparse

from loguru import logger

from ios_cov_manager.model.branch_coverage import BranchCoverage
from ios_cov_manager.framework.version_build_coverage_manager import (
    VersionBuildCoverageManager,
)
from ios_cov_manager.framework.coverage_platform_manager import CoveragePlatformManager


if __name__ == "__main__":
    # Create an argument parser
    parser = argparse.ArgumentParser(description="iOS Coverage Manager")

    # Add arguments to the parser
    parser.add_argument(
        "--ios_repo_git_url",
        type=str,
        help="The Git URL of the iOS project repository",
    )
    parser.add_argument(
        "--branch_name",
        type=str,
        help="The branch name of the iOS build artifact",
    )
    parser.add_argument(
        "--version",
        type=str,
        help="The build version of the iOS build artifact",
    )
    parser.add_argument(
        "--build_number",
        type=str,
        help="The build version of the iOS build artifact",
    )
    parser.add_argument(
        "--commit_id",
        type=str,
        help="The commit ID of the iOS build artifact",
    )
    parser.add_argument(
        "--build_machine_repo_path",
        type=str,
        help="The path of the iOS project repository on the build machine",
    )
    parser.add_argument(
        "--platform_base_url",
        type=str,
        help="The base URL of the coverage platform",
    )
    parser.add_argument(
        "--options",
        type=str,
        default="",
        help="Open the index.html file after parsing",
    )

    # Parse the command-line arguments
    args = parser.parse_args()
    logger.info(f"Args: {args}")
    ios_repo_git_url = args.ios_repo_git_url
    branch_name = args.branch_name
    commit_id = args.commit_id
    version = args.version
    build_number = args.build_number
    build_machine_repo_path = args.build_machine_repo_path
    platform_base_url = args.platform_base_url
    options = args.options
    op_list = options.split(",")
    OPEN_HTML = False
    if len(op_list) >= 1:
        if op_list[0] == "open_html":
            OPEN_HTML = True

    # Check if all required arguments are provided
    if not all(
        [
            ios_repo_git_url,
            branch_name,
            commit_id,
            version,
            build_machine_repo_path,
        ]
    ):
        raise ValueError("Missing required arguments")

    branch_coverage_data = {
        "ios_repo_git_url": ios_repo_git_url,
        "branch_name": branch_name,
        "commit_id": commit_id,
        "version": version,
        # "build_number": build_number,
        "build_machine_repo_path": build_machine_repo_path,
    }

    # Create the branch coverage data object
    branch_coverage = BranchCoverage(**branch_coverage_data)

    # Create a version and build level coverage data manager
    manager = VersionBuildCoverageManager(**branch_coverage_data)

    # Create a coverage platform manager
    cpm = CoveragePlatformManager(
        platform_base_url=platform_base_url,
        branch_coverage=branch_coverage,
    )

    # Download the artifacts and deploy them
    cpm.download_profraws_and_artifacts()

    # Calculate the build level coverage data
    if build_number:
        build_level_coverage_data = manager.calc_build_level_coverage(
            build_number=build_number
        )
        logger.success(f"build_level_coverage_data: {build_level_coverage_data}")

    # Calculate the version level coverage data
    version_level_coverage_data = manager.calc_version_level_coverage()
    logger.success(f"version_level_coverage_data: {version_level_coverage_data}")
